package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.SysLoginLogDTO;
import com.andy.blog.admin.service.SysLoginLogBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.dal.vo.SysLoginLogVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/monitor/loginlog")
public class SysLoginLogController {

    @Resource
    private SysLoginLogBusService sysLoginLogBusService;

    /**
     * 分页查询
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/selectListByPage")
    public GeneralResponseResult<PageUtilsDTO<SysLoginLogDTO>> selectListByPage(SysLoginLogVO loginLogVO) {
        return GeneralResponseResult.success(sysLoginLogBusService.selectListByPage(loginLogVO));
    }

    /**
     * 导出
     */
    @Log(title = "登录日志", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogVO sysLoginLogVO) {
        List<SysLoginLogDTO> list = sysLoginLogBusService.selectListByCondition(sysLoginLogVO);
        EasyExcelUtils.writeStyleExcel(response, list, "登录日志", SysLoginLogDTO.class);
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{ids}")
    public GeneralResponseResult<Void> remove(@PathVariable List<Long> ids) {
        sysLoginLogBusService.deleteByIds(ids);
        return GeneralResponseResult.success();
    }

    /**
     * 清空
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/clean")
    public GeneralResponseResult<Void> clean() {
        sysLoginLogBusService.clean();
        return GeneralResponseResult.success();
    }

    /**
     * 账户解锁
     */
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessTypeEnum.OTHER)
    @GetMapping("/unlock/{userName}")
    public GeneralResponseResult<Void> unlock(@PathVariable("userName") String userName) {
        sysLoginLogBusService.clearLoginRecordCache(userName);
        return GeneralResponseResult.success();
    }
}
